% Counts the number of DICOM files in a director
function [count,sequenceNum] = countDICOMFiles(Dir_Str)

temp        = dir(Dir_Str);
count       = 0;
sequenceNum = 0;


% loop through the files in the directory
for i =1:1:size(temp,1)

   % Irgnoe file names less than 3 chars long (ie . and ..) 
   if length(temp(i).name) >=3
       
       % If file names begine with 'IM-' it is counted as a DICOM file
       if strcmp(temp(i).name(1:3),'IM-')
       
           % consistency checking
           if count == 0
              
              % Sequence number consistency 
              FirstSeriesNumber = str2num(temp(i).name(4:7)); 
           else
              
              % Sequence number consistency 
              if FirstSeriesNumber ~= str2num(temp(i).name(4:7))
              
                  disp([ 'CountDICOM - Error. Sequence number inconsistency. ' ...
                         'Check directory contents sequence numbers.']);
                  count = 0;
                  sequenceNum = 0;
                  return;
              end
           end
           
           % Increase DICOM count.
           count = count + 1;
       end 
   end 
end

sequenceNum = FirstSeriesNumber;